/*
128.最长连续序列 https://leetcode.cn/problems/longest-consecutive-sequence/
2024/10/06 中等
先对给定的整数数组排序，然后遍历数组，遇到连续的数字就增加当前连续长度，遇到不连续的就更新最长连续长度并重置当前长度，最后返回最长连续长度。
*/
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int len = nums.size();
        if (len == 0||len == 1) return len;
        sort(nums.begin(), nums.end());
        int res = 1;
        int curLen = 1;
        for (int i = 1; i < len; i++) {
            if (nums[i] == nums[i - 1]) {
                continue;
            } else if (nums[i] == nums[i - 1]+1) {
                curLen++;
            } else {
                res = max(res, curLen);
                curLen = 1;
            }
        }
        return max(res, curLen);
    }
};